package com.offcn.sellergoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.entity.PageResult;
import com.offcn.sellergoods.dao.BrandMapper;
import com.offcn.sellergoods.pojo.Brand;
import com.offcn.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {

    @Autowired(required = false)
    @Resource
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        // 使用自动注入 Mapper 的方式
//        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
//        return brandMapper.selectList(queryWrapper);

        // 使用继承ServiceImpl 来实现的操作
        List<Brand> list = this.list();
        return list;
    }

    @Override
    public Brand findById(Long id) {
        return this.getById(id);
    }

    @Override
    public void add(Brand brand) {
        this.save(brand);
    }

    @Override
    public void update(Brand brand) {
        this.updateById(brand);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public List<Brand> findList(Brand brand) {
        QueryWrapper<Brand> queryWrapper = this.createQueryWrapper(brand);
        return this.list(queryWrapper);
    }

    private QueryWrapper<Brand> createQueryWrapper(Brand brand){
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if (brand != null) {
            if (brand.getId() != null){
                queryWrapper.eq("id",brand.getId());
            }
            if (brand.getFirstChar() != null && !brand.getFirstChar().isEmpty()) {
                queryWrapper.eq("first_char",brand.getFirstChar());
            }
            if (brand.getName() != null && !brand.getName().isEmpty()) {
                queryWrapper.like("name",brand.getName());
            }
        }
        return queryWrapper;
    }

    @Override
    public PageResult<Brand> findPage(int pageNo, int pageSize) {
        Page<Brand> myPage = new Page<>(pageNo, pageSize);
        IPage<Brand> page = this.page(myPage, new QueryWrapper<Brand>());
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public PageResult<Brand> findPage(Brand brand, int pageNo, int pageSize) {
        QueryWrapper<Brand> queryWrapper = createQueryWrapper(brand);
        Page<Brand> myPage = new Page<>(pageNo, pageSize);
        IPage<Brand> page = this.page(myPage, queryWrapper);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public List<Map> selectOptionBrand() {
        return brandMapper.selectOptionBrand();
    }
}
